<?php

Yii::import('application.models._base.BaseComment');

class Comment extends BaseComment
{
	public static function model($className=__CLASS__) {
		return parent::model($className);
	}
    public function getComment($share_id,$first,$total){
        $db = Yii::app()->db->createCommand();
        $info = $db
            ->select('u.nick_name,u.domain,c.uid,c.content,c.create_time,c.id')
            ->from('{{comment}} c')
            ->leftJoin('{{user}} u','c.uid=u.id')
            ->where('share_id=:share_id',array(':share_id'=>$share_id))
            ->order('c.create_time desc')
            ->limit($total,$first)
            ->queryAll();
        return $info;
    }
    public function setShareComment($share_id){
        $db = Yii::app()->db->createCommand();
        $info = $db
            ->select('c.id,c.share_id,c.content,c.uid,u.nick_name,u.domain')
            ->from('{{comment}} c')
            ->leftJoin('{{user}} u','c.uid=u.id')
            ->where('c.share_id=:share_id',array(':share_id'=>$share_id))
            ->order('c.create_time asc')
            ->queryAll();
        $info_array = serialize($info);
        $share_info = ShareComment::model()->findByPk($share_id);
        $share_info->comment = $info_array;
        $share_info->update();
        //写入share表
        $count = Comment::model()->count('share_id=:share_id',array(':share_id'=>$share_id));
        $share = Share::model()->findByPk($share_id);
        $share->total_comments = intval($count);
        $share->update();
    }
    public function createComment($share_id,$uid,$content,$nick_name,$domain){
        $db = new Comment();
        $db->uid = $uid;
        $db->share_id = $share_id;
        $db->content = $content;
        $db->create_time = time();
        $share_data = Share::model()->findByPk($share_id);
        if($share_data){
            if($db->save()){
                $id = $db->attributes['id'];
                $data['status'] = 1;
                $data['message'] = '发布评论成功';
                $data['id'] = $id;
                $data['share_id'] = $share_id;
                $data['content'] = $content;
                $data['uid'] = $uid;
                $data['nick_name'] = $nick_name;
                //写入share_comment表
                $share_info = ShareComment::model()->findByPk($share_id);
                $share_arr = $arr = array();
                $arr['id'] = $id;
                $arr['share_id'] = $share_id;
                $arr['content'] = $content;
                $arr['uid'] = $uid;
                $arr['nick_name'] = $nick_name;
                $arr['domain'] = $domain;
                if($share_info && $share_info->comment){
                    $share_arr = unserialize($share_info->comment);
                    $share_arr[] = $arr;
                    $comments = serialize($share_arr);
                    $share_info->comment = $comments;
                    $share_info->save();
                }else{
                    $share_arr[] = $arr;
                    $comments = serialize($share_arr);
                    $share_comment_db = new ShareComment();
                    $share_comment_db->id = $share_id;
                    $share_comment_db->comment = $comments;
                    $share_comment_db->save();
                }
                //写入share表
                $count = Comment::model()->count('share_id=:share_id',array(':share_id'=>$share_id));
                $share = Share::model()->findByPk($share_id);
                $share->total_comments = intval($count);
                $share->update();
                //写入事件表
                Event::model()->addEvent(
                    'event',
                    'comment',
                    $uid,
                    $share_data->uid,
                    $share_id,
                    '评论'
                );
            }else{
                $data['status'] = 0;
                $data['message'] = '发布评论失败';
            }
        }else{
            $data['status'] = 0;
            $data['message'] = '没有找到这个分享';
        }
        return $data;
    }
}